package com.hermit.qmst.UI.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.RequestHttp;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by Administrator on 2017/1/3 0003.
 */
@ContentView(R.layout.new_activity_up_name)
public class UpNameActivity extends Activity implements View.OnClickListener {
    @ViewInject(R.id.iv_back)
    ImageView mIvBack;
    @ViewInject(R.id.tv_title)
    TextView mTvTitle;
    @ViewInject(R.id.iv_right)
    TextView mIvRight;
    @ViewInject(R.id.et_name)
    EditText mEtName;
    private String upUrl = Common.mUrl + "/szdh/userInfo.php?type=update&uid=" + Common.mUserId+"&code=";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mIvBack.setOnClickListener(this);
        mIvRight.setText("保存");
        mIvRight.setVisibility(View.VISIBLE);
        mIvRight.setOnClickListener(this);
        mTvTitle.setText(UserInfoActivity.Title);
        mEtName.setHint(UserInfoActivity.nameTExt);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_right:
                save();
                break;
        }
    }

    private void save() {
        String url = "";
        String name = mEtName.getText().toString().trim();
        if (UserInfoActivity.isName) {
            url = upUrl + "nikeName&param=";
        } else {
            url = upUrl + "autograph&param=";
        }
        try {
            url += URLEncoder.encode(name, "utf-8");
        } catch (UnsupportedEncodingException e) {

        }
        RequestHttp.getHttp(this, url, new RequestHttp.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject) {
                try {
                    if (jsonObject.getBoolean("state")) {
                        Toast.makeText(UpNameActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(UpNameActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {

                }
            }
        });
    }
}
